<script type="text/javascript"><!--//--><![CDATA[//><!--

	var grid_reload = function(){
		$('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
	};

	$(document).ready(function() {
		// Setup Datagrid
		$('#datagrid').datagrid({  
			url:gs_path+'/'+controller+'/getdata/?param=<?php echo get('param'); ?>', 
			queryParams: $('#fm_search').serializeObject(),
			height:340,
			pagination:true,
			columns:[
				[	{ field : 'ck', checkbox:true, width: 40, align: 'center', rowspan:2 },
					{ field : 'tgl_efektif', title : 'Tgl. Efektif', align:'center', width : 80, formatter:formatDate, sortable:true, rowspan:2 },
					{ field : 'param', hidden : true, rowspan:2 }<?php
					for($i=0, $max_i=count($arr_jenis_anggota); $i<$max_i; $i++){
						$row = $arr_jenis_anggota[$i];
							echo ",{ title: '".$row['nama_jenis_anggota']."', align:'center', colspan:".count($arr_variabel)." }";
						}
					?>],
				[
					<?php
					$ln_cellwidth	= 100;
					if(get('param') == 'bunga'){
						$ln_cellwidth = 120;
					}else if(get('param') == 'saldo'){
						$ln_cellwidth = 150;
					}else if(get('param') == 'biaya'){
						$ln_cellwidth = 125;
					}
					for($j=0, $max_j=count($arr_jenis_anggota); $j<$max_j; $j++){
						$row_jnsagt = $arr_jenis_anggota[$j];
						$key	= strtolower($row_jnsagt['kd_jenis_anggota']); 
						for($i=0,$max_i=count($arr_variabel); $i<$max_i; $i++){
							$row = $arr_variabel[$i];
							
							if($i>0 || $j>0){ echo ','; }
							echo "{ field : '".strtolower(str_replace('.','_',$row['id_variabel']))."_".$key."', title : '".$row['nama_variabel']."', align:'right', width : ".$ln_cellwidth.", formatter:function(val){ return formatNumeric(val,2); }, sortable:true }";
						}
					} ?>
				]
			]
		});
		
		// Setup Form
		$('#form_master').setupForm([
			{ name: 'tgl_efektif', type: 'date', noempty:true }
			<?php
			for($j=0, $max_j=count($arr_jenis_anggota); $j<$max_j; $j++){
				$row_jnsagt = $arr_jenis_anggota[$j];
				$key	= strtolower($row_jnsagt['kd_jenis_anggota']); 
				for($i=0,$max_i=count($arr_variabel); $i<$max_i; $i++){
					$row = $arr_variabel[$i];
					if(substr(strtolower($row['id_variabel']),0,5)=='bunga'){
						echo ",{ name : '".strtolower(str_replace('.','_',$row['id_variabel']))."_".$key."', type : 'numeric', precision:0, maxlength:3, size:5, noempty : false }";
					}else{
						echo ",{ name : '".strtolower(str_replace('.','_',$row['id_variabel']))."_".$key."', type : 'numeric', precision:0, noempty : false }";
					}
				}
			}?>
		]);
		
		
		// Create Dialog
		$('#dialog_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:600,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					$('#form_master').submitForm('/'+controller+'/ajax_post/?param=<?php echo get('param'); ?>', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); }} );
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dialog_container').dialog('close');
				}
			}]
		});
		
		
		// Set Action Button
		var buttons = [ { label: 'Add', icon: 'add', onclick: function() {
							resetError();
							$('#dialog_container').dialog('open');
							$('#form_master').data('mode', 'create');
							$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Parameter Baru');
							$('#tgl_efektif').readonly(false);
							$('#form_master').resetForm();
							$('#form_master').focusFirst();
						} },
						{ label: 'Edit', icon: 'edit', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{ 
								resetError();
								$('#dialog_container').dialog('open'); 
								$('#form_master').data('mode', 'edit');
								$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Ubah Parameter');
								$('#tgl_efektif').readonly();
								jsonToForm(oSel); $('#form_master').focusFirst();
							} 
						} },
						{label: 'Delete', icon: 'delete', onclick: function() { 
							var oSel = $('#datagrid').datagrid('getSelected'); 
							if (oSel == null) { 
								$.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
							} else { 
								deleteData(['tgl_efektif', 'param']);
							} 
						}}];
		setAction(buttons);
		
		
		$('#fm_search').focusFirst();
	});
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div style="display:none">
        <form id="fm_search" onsubmit="grid_reload(); return false;">
        	<table class="tb_form" style="margin-bottom:0px">
            <tr>
            	<td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                	<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>


<div id="dialog_container" style="padding:5px;width:600px;height:<?php $ln_height = 200+(30*count($arr_variabel)*count($arr_jenis_anggota)); echo ($ln_height > 450 ? 450 : $ln_height); ?>px;">
	<form id="form_master">
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td class="label" style="width:150px">Tgl. Efektif</td>
        <td><input id="tgl_efektif" /></td>
    </tr>
    <?php
	for($j=0, $max_j=count($arr_jenis_anggota); $j<$max_j; $j++){
		$row_jnsagt = $arr_jenis_anggota[$j];
		$key	= strtolower($row_jnsagt['kd_jenis_anggota']); 
		echo '<tr>';
			echo '<td colspan="2" style="border-top:#ccc solid 1px"><b>'.$row_jnsagt['nama_jenis_anggota'].'</b></td>';
		echo '</tr>';
		for($i=0,$max_i=count($arr_variabel); $i<$max_i; $i++){
			$row = $arr_variabel[$i];
			
			echo '<tr>';
			echo '<td class="label">'.$row['nama_variabel'].'</td>';
			echo '<td><input id="'.strtolower(str_replace('.','_',$row['id_variabel'])).'_'.$key.'" /> '.(substr(strtolower($row['id_variabel']),0,5)=='bunga' ? '%' : '').' </td>';
			echo '</tr>';
		} 
	} ?>
    </table>
	</form>
</div>